Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Text.RegularExpressions

Namespace C4F.DevKit.Calendar.Parser
	''' <summary>
	''' A class containing methods to help with parsing iCal files.
	''' </summary>
	Public Class ParserHelper
		''' <summary>
		''' Removes the iCal escape characters from a string, as defined by RFC 2445.
		''' </summary>
		''' <param name="input">The string from which to remove escape characters.</param>
		''' <returns>The input string with escape characters removed.</returns>
		Public Shared Function RemoveEscapeChars(ByVal input As String) As String
			Dim output As String = ""
			Dim isEscapeCharacter As Boolean = False

			For Each character As Char In input
				If character = "\"c AndAlso ((Not isEscapeCharacter)) Then
					isEscapeCharacter = True
				ElseIf isEscapeCharacter AndAlso character.ToString().ToUpper().Equals("N") Then
					output = output & ControlChars.Lf
					isEscapeCharacter = False
				Else
					output = output & character
					isEscapeCharacter = False
				End If
			Next character

			Return output
		End Function

		''' <summary>
		''' Inserts escape characters in the appropriate places in the string as specified
		''' by RFC 2445.
		''' </summary>
		''' <param name="input">The string into which to insert escape characters.</param>
		''' <returns>The input string with escape characters inserted.</returns>
		Public Shared Function InsertEscapeChars(ByVal input As String) As String
			Dim result As String = ""

			For Each character As Char In input
				If "\" & Constants.vbLf & ";,".IndexOf(character) = -1 Then
					result = result & character
				ElseIf character = ControlChars.Lf Then
					result = result & "\n"
				Else
					result = result & "\"c + character
				End If
			Next character

			Return result
		End Function

		''' <summary>
		''' Finds an attendee name on an ATTENDEE line in an iCal file.
		''' </summary>
		''' <param name="line">The ATTENDEE line to search.</param>
		''' <returns>The name of the attendee.</returns>
		Public Shared Function GetAttendeeName(ByVal line As String) As String
			Return Regex.Match(line, "CN=([^;:," & ChrW(&H8).ToString() & ChrW(&HA1F).ToString() & ChrW(&H7F).ToString() & """]+|""[^""]+"")").Groups(1).Value.Trim(""""c)
		End Function

		''' <summary>
		''' Gets an e-mail address from a line in an iCal file with a MAILTO property
		''' </summary>
		''' <param name="line">The input line to search.</param>
		''' <returns>The e-mail address.</returns>
		Public Shared Function GetEmailAddress(ByVal line As String) As String
			Return Regex.Match(line, "MAILTO:(.*)$").Groups(1).Value
		End Function

		''' <summary>
		''' Parses the parts out of a string representing a list of weekdays in a month.
		''' </summary>
		''' <param name="line">The string to parse.</param>
		''' <returns>The weeknumber and weekday abbreviation.</returns>
		Public Shared Function GetMonthWeekdayParts(ByVal line As String) As String()
			Dim matchGroups As GroupCollection = Regex.Match(line, "((?:\+|-)?\d\d?)?(SU|MO|TU|WE|TH|FR|SA)").Groups
			Dim result As String() = New String(1){}
			result(0) = matchGroups(1).Value
			result(1) = matchGroups(2).Value
			Return result
		End Function

		''' <summary>
		''' Parses the parts of a string representing a duration.
		''' </summary>
		''' <param name="line">The string to parse.</param>
		''' <returns>The count and the increment abbreviation, both in string form.</returns>
		Public Shared Function GetDurationParts(ByVal line As String) As String()
			Dim groups As GroupCollection = Regex.Match(line, "(\d+)([WDHMS])").Groups
			Dim results As String() = New String(1){}
			results(0) = groups(1).Value
			results(1) = groups(2).Value
			Return results
		End Function
	End Class
End Namespace
